import { http, setToken } from '@/utils'
import { SET_TOKEN } from '@/store/actionTypes/token'

// 保存token
const userLoginSetToken = token => {
  return {
    type: SET_TOKEN,
    payload: token
  }
}

// 用户登录
export const userLogin = data => {
  return async dispatch => {
    const {
      message,
      data: { token }
    } = await http.post('authorizations', data)

    if (message === 'OK') {
      // 保存token到本地
      setToken(token)

      dispatch(userLoginSetToken(token))
    }
  }
}
